/*
  test12131.c - Linux Paging Algorithm Test File
  Ben Marks, Chris Lekas

  Allocate 90,000 pages, divided into three main chunks:
  => Section 1: Pages 0 - 29,999
  => Section 2: Pages 30,000 - 59,999
  => Section 3: Pages 60,000 - 89,999

  Accesses sections 1, 2, 1, 3 in order. Then, counts
  number of faults on subsequent access to section 1. 

 */



#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include <linux/pgfltstats.h>

#include "common.h"
#include "sections.h"


/*
  Helper function to access sections 1, 2, 1, 3 in order.
 */
void setupAccess() {

  accessFirst();
  accessSecond();
  accessFirst();
  accessThird();


}

/*
  Access sections 1, 2, 1, 3, and then count number of faults on
  access to section 1, reporting results. 
 */
int main(int argc, char ** argv) {
  

  init();
  setupAccess(); // Access sections 1, 2, 1, 3
  
  // Count number of pagefaults encountered in access to 1
  struct pf_info_struct before;
  if ( pgfltstats( getpid(), PGFLTSTAT_PROC, &before ) ) {
    perror("pgfltstats");
  }

  accessFirst(); 

  struct pf_info_struct after;
  if ( pgfltstats( getpid(), PGFLTSTAT_PROC, &after ) ) {
    perror("pgfltstats");
  }

  // Report results
  printf("12131\t%lu\t%lu\n", after.major - before.major,
	 after.minor - before.minor );


}
